<?php
class Admin extends AppModel {
    var $name = 'Admin';
    var $pagesTable    = 'pages';
    var $dienstenTable = 'diensten';
    var $newsTable     = 'nieuwsitems';
    var $imageTable    = 'images';
    
    public $validate = array(
            'datum' => array(
                    'rule' => 'date',
                    'message' => 'vul een datum in (yyyy-mm-dd)'
            ),
            'titel' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Titel mag niet leeg zijn'
            ),
            'content' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Content mag niet leeg zijn'
            )
    );

    /*
    ===============================================================================
    
        Pages
    
    ===============================================================================
    */
    public function getPages() {
        $queryStr = "SELECT page, title
        FROM `{$this->pagesTable}`";
        $result = $this->query( $queryStr );
    
        return $result;
    }
    
    public function getPage( $page ) {
        $queryStr = "SELECT page, title, content
            FROM `{$this->pagesTable}`
            WHERE `page` = '$page'";
        
        $result = $this->query( $queryStr );
        $content = array();
//         debug($queryStr);
//         debug($result);
        
        if( isset( $result ) && !empty( $result ) ) {
            foreach( $result as $res ) {
                $temp            = $res[$this->pagesTable];
                $temp['page']   = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['page'] ) ) );
                $temp['titel']   = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['title'] ) ) );
                $temp['content'] = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['content'] ) ) );
                $content         = $temp;
            }
        }

        return $content;
    }
    
    public function savePage( $data ) {
        $page    = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['page'] ) ) );
        $title   = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['titel'] ) ) );
        $content = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['content'] ) ) );
        
        $queryStr = "UPDATE `{$this->pagesTable}`
             SET `title`   = '$title',
                 `content` = '$content'
             WHERE `page` = '$page'";

        $result = $this->query( $queryStr );
    }

    
    /*
    ===============================================================================
    
    Diensten
    
    ===============================================================================
    */
    
    public function saveDienst( $data ) {
        debug($data);
    
        $page    = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['type'] ) ) );
        $title   = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['titel'] ) ) );
        $content = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['content'] ) ) );

        $queryStr = "UPDATE `{$this->dienstenTable}`
        SET `title`   = '$title',
        `content` = '$content'
        WHERE `type` = '$page'";
    
        $result = $this->query( $queryStr );
    }

    public function getDienst( $dienst ) {
        $queryStr = "SELECT type, title, content
        FROM `{$this->dienstenTable}`
        WHERE `type` = '$dienst'";
    
        $result = $this->query( $queryStr );
        $content = array();
        //         debug($queryStr);
        //         debug($result);
    
        if( isset( $result ) && !empty( $result ) ) {
            foreach( $result as $res ) {
                $temp            = $res[$this->dienstenTable];
                $temp['type']    = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['type'] ) ) );
                $temp['titel']   = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['title'] ) ) );
                $temp['content'] = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['content'] ) ) );
                $content         = $temp;
            }
        }
    
        return $content;
    }
    
    public function getDiensten() {
        $queryStr = "SELECT type, title
        FROM `{$this->dienstenTable}`";
        $result = $this->query( $queryStr );
    
        return $result;
    }

    /*
    ===============================================================================
    
    Nieuwsitems
    
    ===============================================================================
    */
    
    public function addNewsItem( $data ) {
//         debug( $data );
        
        if ( !empty( $data ) ) {
            $date    = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['datum'] ) ) );
            $title   = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['titel'] ) ) );
            $content = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['content'] ) ) );
            $image   = '';
            
            $myQuery = "INSERT INTO `{$this->newsTable}` (`key`, `title`, `content`, `image`, `date`)
                            VALUES (NULL, '$title', '$content', '$image', '$date')";
            $this->query( $myQuery );
            
            $key = $this->query( "SELECT LAST_INSERT_ID()"); 
            return $key[0][0]['LAST_INSERT_ID()'];
        }
    }
    
    public function getNewsItems() {
        $myQuery = "SELECT title, date, `{$this->newsTable}`.key
                        FROM `{$this->newsTable}`";
        $result = $this->query( $myQuery );
        
        return $result;
    }
    
    public function getNewsItem( $item ) {
        $myQuery = "SELECT date, title, content, image, `{$this->newsTable}`.`key`
                        FROM `{$this->newsTable}`
                        WHERE `{$this->newsTable}`.`key` = '$item'";
        
        $result = $this->query( $myQuery );
        
        $myQuery = "SELECT image
                        FROM `{$this->imageTable}`
                        WHERE `{$this->imageTable}`.`type` = 'newsItem'
                        AND `{$this->imageTable}`.`post` = '$item'";

        $images = $this->query( $myQuery );
        
//         debug( $images );
        
        $content = array();
        //         debug($queryStr);
        //         debug($result);
        
        if( isset( $result ) && !empty( $result ) ) {
                foreach( $result as $res ) {
                $temp            = $res[$this->newsTable];
                $temp['datum']   = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['date'] ) ) );
                $temp['titel']   = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['title'] ) ) );
                $temp['content'] = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['content'] ) ) );
                $temp['image']   = stripslashes( html_entity_decode( htmlspecialchars_decode( $temp['image'] ) ) );
                $content         = $temp;
            }
        }
        
        return $content;
    }
    
    public function deleteNewsItem( $key ) {
        $myQuery = "DELETE FROM `{$this->newsTable}`
                        WHERE `{$this->newsTable}`.`key` = '$key'";
        $this->query( $myQuery );
    }
    
    public function saveNewsItem( $data ) {
//         debug( $data );
        
        $date    = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['datum'] ) ) );
        $title   = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['titel'] ) ) );
        $content = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['content'] ) ) );
        $image   = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['image'] ) ) );
        $key     = stripslashes( html_entity_decode( htmlspecialchars_decode( $data['Admin']['key'] ) ) );
        
        $queryStr = "UPDATE `{$this->newsTable}`
                        SET `title`   = '$title',
                        `content` = '$content',
                        `date` = '$date',
                        `image`   = '$image'
                        WHERE `{$this->newsTable}`.`key` = '$key'";
    
        $result = $this->query( $queryStr );
    }

    /*
     ===============================================================================
    
    Images
    
    ===============================================================================
    */
    
    public function addImages( $data ) {
        $post   =  $data['key'];
        $type   = $data['type'];
        $thumbs = $data['thumbs'];
        
        $i = 1;
        
        foreach ($data['images'] as $image) {
            
            $exploded  = explode('/', $image);            
            $imgName   = end( $exploded );
            $exploded  = explode('/', $thumbs[$i]);
            $thumbName = end( $exploded );
            
            $myQuery = "INSERT INTO `{$this->imageTable}` (`key`, `type`, `post`, `image`, `thumb`)
            VALUES (NULL, '$type', '$post', '$imgName', '$thumbName')";
            
            //echo $myQuery;
            
            $this->query( $myQuery );
            //debug( $image );
            $i++;
        }
    }
    
    public function getImages( $type, $post ) {
        $myQuery = "SELECT * 
                        FROM `{$this->imageTable}` 
                        WHERE `type` = '$type' 
                        AND `post` = $post";
        
        $result = $this->query($myQuery);
        debug( $result );        
        return $result;        
    }
}